<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE HTML>
<html>
<head>
<%@ include file="../../top.jsp"%> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
		<div id="treeDiv">
			<ul id="tree" class="tree" style="overflow:auto;"></ul>
		</div>
		<div style="margin-left:150px;margin-top:50px;">
		<a class="btn btn-mini btn-primary" onclick="save();">保存</a>&nbsp;&nbsp;&nbsp;&nbsp;
		<a class="btn btn-mini btn-danger" onclick="top.Dialog.close();">取消</a>
		</div>
</body>
<script type="text/javascript">
		top.hangge();
		var rootPath =getRootPath();
		var zTree;
		var menu_ids;
		var user_id ='${user_id}';
	$.ajax({
		url : rootPath+'/user/treeData.do',
		type : "post",
		async: false,
		dataType: 'json', 
	    success:function(data){
	    	var zTreeNodes=eval(data.zTreeNodes);
	    	var setting = {
	    			checkable : true,
	    			callback: {
					change: zTreeOnClick
				}, 
			    showLine: true    
			};
			zTree = $("#tree").zTree(setting, zTreeNodes);
	    }
	 });
	
	 var zTreeObj = zTree.getCheckedNodes(true);
	  for (var i = 0; i < zTreeObj.length; i++) {
		  zTree.checkNode(zTreeObj[i], true);
	  }
	  
	function zTreeOnClick(event, treeId, treeNode) {
		var nodes = zTree.getCheckedNodes(true), id = "";
		for ( var i = 0, l = nodes.length; i < l; i++) {
			id += nodes[i].id + ",";
		}
		if (id.length > 0){
			id = id.substring(0, id.length - 1);
		}
		menu_ids = id;
	}
	
	function save(){
		if(menu_ids==null||menu_ids=='undefined'){
			bootbox.alert("请先勾选");
			return false;
		}
		var url =rootPath+'/user/saveSubSys.do';
		var param ={};
		param['menu_ids']=menu_ids;
		param['user_id']=user_id;
		$.post(url,param,function(callback){
			if(callback.isSuccess==true){
				bootbox.dialog("保存成功!", [ {
					"label" : "确定",
					"class" : "btn-small btn-success",
					"callback" : function(e) {
						top.Dialog.close();
					}
				} 
				])
			}else{
				bootbox.dialog("保存失败!", [ {
					"label" : "确定",
					"class" : "btn-small btn-success",
					"callback" : function(e) {
						top.Dialog.close();
					}
				} 
				])
				
			}
		});
	}
	function getRootPath() {
		// 获取当前网址，如： http://localhost:8083/uimcardprj/share/meun.jsp
		var curWwwPath = window.document.location.href;
		// 获取主机地址之后的目录，如： uimcardprj/share/meun.jsp
		var pathName = window.document.location.pathname;
		var pos = curWwwPath.indexOf(pathName);
		// 获取主机地址，如： http://localhost:8083
		var localhostPaht = curWwwPath.substring(0, pos);
		// 获取带"/"的项目名，如：/uimcardprj
		var projectName = pathName.substring(0,
				pathName.substring(1).indexOf('/') + 1);
		return (localhostPaht + projectName);
	}
</script>
</html>